﻿Imports System.Data.Entity
Imports System.Web.Security

<Authorize>
Public Class CouponController
    Inherits System.Web.Mvc.Controller

    Private db As New CouponDBContext

    '
    ' GET: /Coupon/

    Function Index() As ActionResult
        Return View(db.Coupons.ToList())
    End Function

    '
    ' GET: /Coupon/Details/5

    Function Details(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim coupon As Coupon = db.Coupons.Find(id)
        If IsNothing(coupon) Then
            Return HttpNotFound()
        End If
        Return View(coupon)
    End Function

    '
    ' GET: /Coupon/Create

    Function Create(ByVal qr As String) As ActionResult

        Dim salesPromotionID As Integer
        Dim salesPromotionName As String = ""
        Dim salesPromotions As New List(Of SalesPromotion)()
        Dim cc As MVC1.SalesPromotionController = New SalesPromotionController()

        salesPromotions = cc.db.SalesPromotions.ToList()

        For Each item As SalesPromotion In salesPromotions
            If item.Code = qr Then
                salesPromotionID = item.ID
                salesPromotionName = item.Name
            End If
        Next

        ViewData("QR") = qr
        ViewData("SalesPromotionName") = salesPromotionName
        Session("SalesPromotionID") = salesPromotionID

        Dim coupon As Coupon = New Coupon()
        coupon.SalesPromotionID = salesPromotionID
        coupon.UserName = User.Identity.Name
        coupon.Activated = Date.Now
        coupon.Active = True

        Return View(coupon)
    End Function

    '
    ' POST: /Coupon/Create

    <HttpPost()> _
    <ValidateAntiForgeryToken()> _
    Function Create(ByVal coupon As Coupon) As ActionResult
        If ModelState.IsValid Then

            Dim couponSave As Coupon = New Coupon()
            couponSave.SalesPromotionID = Session("SalesPromotionID")
            couponSave.UserName = User.Identity.Name

            Dim dateTimeNow = Date.Now

            couponSave.Activated = dateTimeNow
            couponSave.Used = dateTimeNow
            couponSave.Active = True

            db.Coupons.Add(couponSave)
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If

        Return View(coupon)
    End Function

    '
    ' GET: /Coupon/Edit/5

    Function Edit(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim coupon As Coupon = db.Coupons.Find(id)
        If IsNothing(coupon) Then
            Return HttpNotFound()
        End If
        Return View(coupon)
    End Function

    '
    ' POST: /Coupon/Edit/5

    <HttpPost()> _
    <ValidateAntiForgeryToken()> _
    Function Edit(ByVal coupon As Coupon) As ActionResult
        If ModelState.IsValid Then
            db.Entry(coupon).State = EntityState.Modified
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If

        Return View(coupon)
    End Function

    '
    ' GET: /Coupon/Delete/5

    Function Delete(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim coupon As Coupon = db.Coupons.Find(id)
        If IsNothing(coupon) Then
            Return HttpNotFound()
        End If
        Return View(coupon)
    End Function

    '
    ' POST: /Coupon/Delete/5

    <HttpPost()> _
    <ActionName("Delete")> _
    <ValidateAntiForgeryToken()> _
    Function DeleteConfirmed(ByVal id As Integer) As RedirectToRouteResult
        Dim coupon As Coupon = db.Coupons.Find(id)
        db.Coupons.Remove(coupon)
        db.SaveChanges()
        Return RedirectToAction("Index")
    End Function

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        db.Dispose()
        MyBase.Dispose(disposing)
    End Sub

End Class